{
  "cells": [
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "# Fraction module"
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "It is used to create fractional instance from integers,real,fractions and even from strings"
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "As a fraction contains a numeator as well as denominator therefore to create a fractional instance we need to provide two values when creating the instance. For creating a fractional instance we need to import Fraction class from fraction module"
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "from fractions import Fraction\nprint Fraction(5,8)\nprint Fraction(0,1)",
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": "5/8\n0\n",
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "In the above code i have basically called Fraction class constructor by passing values of numenator and denominator.Actually Fraction constructor takes default arguments of numenator to be 0 and denominator to be 1. Let us illustrate this fact with example."
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "print Fraction()",
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "text": "0\n",
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "As the output comes out to be 0 which clearly states our fact.But when we pass denominator as 0 we will get ZeroDivisionError as follows:"
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "print Fraction(1,0)",
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "error",
          "ename": "ZeroDivisionError",
          "evalue": "Fraction(1, 0)",
          "traceback": [
            "\u001b[0;31m\u001b[0m",
            "\u001b[0;31mZeroDivisionError\u001b[0mTraceback (most recent call last)",
            "\u001b[0;32m<ipython-input-4-90aeb936f164>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0mFraction\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;32m/home/nbuser/anaconda2_501/lib/python2.7/fractions.pyc\u001b[0m in \u001b[0;36m__new__\u001b[0;34m(cls, numerator, denominator)\u001b[0m\n\u001b[1;32m    160\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    161\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mdenominator\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 162\u001b[0;31m             \u001b[0;32mraise\u001b[0m \u001b[0mZeroDivisionError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Fraction(%s, 0)'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mnumerator\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    163\u001b[0m         \u001b[0mg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgcd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnumerator\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdenominator\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    164\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_numerator\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnumerator\u001b[0m \u001b[0;34m//\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;31mZeroDivisionError\u001b[0m: Fraction(1, 0)"
          ]
        }
      ]
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "### Creating Fraction instance from another Fraction"
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "requires other fraction as instance of numbers as argument and returns Fractional instance of same value(input value)"
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "print Fraction(99/45)",
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "stream",
          "text": "2\n",
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "### Creating Fraction instance from float"
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "requires float instance as argument and returns Fractional instance of same value(input value)"
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "print Fraction(2.56)",
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "stream",
          "text": "5764607523034235/2251799813685248\n",
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "### Creating Fraction instance from decimal"
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "requires decimal instance as argument and returns Fractional instance of same value(input value)"
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "print Fraction('5.99')",
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "stream",
          "text": "599/100\n",
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "### Creating Fraction instance from string"
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "requires string instance as argument and returns Fractional instance of same value(input value)"
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "print Fraction('344/455')",
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "stream",
          "text": "344/455\n",
          "name": "stdout"
        }
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "name": "python2",
      "display_name": "Python 2",
      "language": "python"
    },
    "language_info": {
      "mimetype": "text/x-python",
      "nbconvert_exporter": "python",
      "name": "python",
      "pygments_lexer": "ipython2",
      "version": "2.7.14",
      "file_extension": ".py",
      "codemirror_mode": {
        "version": 2,
        "name": "ipython"
      }
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}
